<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Db;

/**
 * 会员接口
 */
class Project extends Api
{
    protected $noNeedLogin = ['bigscreen','myProject','deviceListByPhone'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();


    }


    public function list()
    {
        $status = input('status',0);
        $title = input('title','');
        $page = input('page',1);
        $limit = input('pageSize',10);
        $where = [];
        $where['user_id'] = $this->auth->id;
        if ($status) {
            $where['status'] = $status;
        } else {
            $where['status'] = ['>',0];
        }
        if ($title) {
            $where['title'] = ['like','%'.$title.'%'];
        }
        $list = Db::name('project_list')->where($where)->page($page,$limit)->select();
        foreach ($list as $key => &$value) {
            $value['img'] = config('upload_domain').$value['img'];
        }
        $total = Db::name('project_list')->where($where)->count();
        $this->success('',['total'=>$total,'list'=>$list]);
    }


    public function release()
    {
        $status = input('status',0);
        $info = input('info','');
        $title = input('title','');
        $img = input('img','');
        if (!$status || !$info) {
            $this->error('缺少参数');
        }
        $id = input('id',0);
        Db::startTrans();
        // try {
            if (!$id) {  // 新建
                $data = [
                    'title' => $title,
                    'status' => $status,
                    'img' => $img,
                    'user_id'=> $this->auth->id,
                ];
                $insert_id = Db::name('project_list')->insertGetId($data);
                if (!$insert_id) {
                    Db::rollback();
                    $this->error('创建项目失败');
                }
                $datainfo = [
                    'project_id' => $insert_id,
                    'content' => $info,
                    'status' => 1,
                ];
                $insert = Db::name('project_info')->insertGetId($datainfo);
                if (!$insert) {
                    Db::rollback();
                    $this->error('创建项目失败');
                }
                $data = [
                    'id' => $insert_id,
                    'content' => json_decode(html_entity_decode($info),1),
                    'title' => $title,
                    'img' => $img,
                    'status' => $status,
                ];
            } else {  // 更新
                $list = Db::name('project_list')->where(['id'=>$id,'user_id'=>$this->auth->id])->find();
                if (!$list) {
                    Db::rollback();
                    $this->error('项目不存在');
                }
                // if ($list['status']!=2) {
                //     Db::rollback();
                //     $this->error('状态有误');
                // }
                $project_info = Db::name('project_info')->where('project_id',$id)->find();
                if (!$project_info) {
                    Db::rollback();
                    $this->error('项目信息不存在');
                }
                if ($status==$list['status'] && $info==$project_info['content'] && $title==$list['title'] && $img==$list['img']) {
                    Db::rollback();
                    $this->error('检测到数据没有变化，已忽略更新');
                }
                $data = [
                    'title' => $title,
                    'status' => $status,
                    'img' => $img,
                    'user_id'=> $this->auth->id,
                ];
                $update = Db::name('project_list')->where('id',$id)->update($data);
                // if (!$update) {
                //     Db::rollback();
                //     $this->error('更新项目失败');
                // }
                $datainfo = [
                    'project_id' => $id,
                    'content' => $info,
                    'status' => 1,
                ];
                $update = Db::name('project_info')->where('project_id',$id)->update($datainfo);
                // if (!$update) {
                //     Db::rollback();
                //     $this->error('更新项目失败');
                // }
                $data = [
                    'id' => $id,
                    'content' => json_decode(html_entity_decode($info),1),
                    'title' => $title,
                    'img' => $img,
                    'status' => $status,
                ];
            }
            Db::commit();
        // } catch (\Exception $e) {
        //     Db::rollback();
        //     $this->error($e->getMessage());
        // }
        $this->success('成功',$data);
    }


    public function info()
    {
        $id = input('id/d',0);
        if (!$id) {
            $this->error('缺少参数');
        }
        $where = [];
        $where['id'] = $id;
        $where['status'] = ['>',0];
        $list = Db::name('project_list')->where($where)->find();
        if (!$list) {
            $this->error('项目不存在');
        }
        $info = Db::name('project_info')->where('project_id',$id)->field('content')->find();
        $info = array_merge($list,$info);
        $info['content']=html_entity_decode($info['content']);// 反转义
        $content = json_decode($info['content']);
        $info['content'] = $content;
        // $info['content']=stripslashes($info['content']);
        // $info['content']=str_replace("\\", "", $info['content']);
        $this->success('成功',$info);
    }



    public function modify()
    {
        $status = input('status',0);
        $id = input('id',0);
        if (!$status || !$id) {
            $this->error('缺少参数');
        }
        if (!in_array($status, [1,2,-1])) {
            $this->error('状态有误');
        }
        $list = Db::name('project_list')->where('id',$id)->find();
        if (!$list) {
            $this->error('项目不存在');
        }
        // 发布的只能改为草稿，草稿可以编辑或删除
        if ($status==-1 && $list['status']==1) {
            $this->error('只能删除草稿项目');
        }
        if ($status==1 && $list['status']==-1) {
            $this->error('发布失败');
        }
        if ($status==2 && $list['status']==-1) {
        }
        $update = Db::name('project_list')->where('id',$id)->update([
            'status' => $status,
            'update_dt' => date('Y-m-d H:i:s'),
        ]);
        if (!$update) {
            $this->error('更新失败');
        }
        $list = Db::name('project_list')->where('id',$id)->find();
        $info = Db::name('project_info')->where('project_id',$id)->field('content')->find();
        $info = array_merge($list,$info);
        $this->success('成功',$info);
    }



    public function deviceList()
    {
        // $admin = Db::name('admin')->where(['username'=>$this->auth->username,'status'=>'normal'])->find();
        // if (!$admin) {
        //     $this->error('账户信息有误，请联系管理员');
        // }
        // $list = Db::name('device')->where(['admin_id'=>$admin['id']])->find();
        // $content = json_decode($list['content'],1);
        // $contentlist = [];
        // foreach ($content['list'] as $k => &$v) {
        //     if (isset($v['children'])) {
        //         foreach ($v['children'] as $key => &$value) {
        //             $value['img'] = '';
        //             $row = Db::name('device_list')->where('device_id',$value['id'])->find();
        //             if (isset($row['img'])) {
        //                 $value['img'] = config('upload_domain').$row['img'];
        //             }
        //             $contentlist[] = $value;
        //         }
        //     } else {
        //         $v['img'] = '';
        //         $row = Db::name('device_list')->where('device_id',$v['id'])->find();
        //         if (isset($row['img'])) {
        //             $v['img'] = config('upload_domain').$row['img'];
        //         }
        //         $contentlist[] = $v;
        //     }
        // }
        // $this->success('成功',$contentlist);
        // // foreach ($content['list'] as $k => &$v) {
        // //     $v['img'] = '';
        // //     $row = Db::name('device_list')->where('device_id',$v['id'])->find();
        // //     if (isset($row['img'])) {
        // //         $v['img'] = config('upload_domain').$row['img'];
        // //     }
        // // }
        // // $this->success('成功',$content['list']);
        $device_list = Db::name('device_list')->where(['admin_id'=>0,'status'=>1,'pid'=>0])->select();
        if ($device_list) {
            foreach ($device_list as $key => &$value) {
                $value['name'] = $value['title'];
                $children = Db::name('device_list')->where(['admin_id'=>0,'status'=>1,'pid'=>$value['id']])->select();
                foreach ($children as $k => &$v) {
                    $v['name'] = $v['title'];
                    $v['img'] = config('upload_domain').$v['img'];
                }
                $value['children'] = $children;
            }
        }
        $this->success('成功',$device_list);
    }



    public function deviceListByPhone()
    {
        $this->success('成功');
        $phone = input('phone',0);
        if (!$phone) {
            $this->error('缺少参数');
        }
        $admin = Db::name('admin')->where(['username'=>$phone,'status'=>'normal'])->find();
        if (!$admin) {
            $this->error('账户信息有误，请联系管理员');
        }
        $list = Db::name('device')->where(['admin_id'=>$admin['id']])->find();
        $content = json_decode($list['content'],1);
        foreach ($content['list'] as $k => &$v) {
            $v['img'] = '';
            $row = Db::name('device_list')->where('device_id',$v['id'])->find();
            if (isset($row['img'])) {
                $v['img'] = config('upload_domain').$row['img'];
            }
        }
        $this->success('成功',$content['list']);
    }

    // 用户端显示大屏详情
    public function bigscreen()
    {
        $id = input('id/d',0);
        if (!$id) {
            $this->error('缺少参数');
        }
        $where = [];
        $where['id'] = $id;
        $where['status'] = 1;
        $list = Db::name('project_list')->where($where)->find();
        if (!$list) {
            $this->error('项目不存在');
        }
        $info = Db::name('project_info')->where('project_id',$id)->field('content')->find();
        $info = array_merge($list,$info);
        $info['content']=html_entity_decode($info['content']);// 反转义
        $content = json_decode($info['content']);
        $info['content'] = $content;
        // $info['content']=stripslashes($info['content']);
        // $info['content']=str_replace("\\", "", $info['content']);
        $this->success('成功',$info);
    }

    // 用户端项目列表
    // 3c31a995-733f-45bf-ae51-78dc371feca8
    public function myProject()
    {
        // $phone = input('phone',0);
        // if (!$phone) {
        //     $this->error('缺少参数');
        // }
        // $user = Db::name('user')->where(['username'=>$phone,'status'=>'normal'])->find();
        $user = Db::name('user')->where(['id'=>$this->auth->id])->find();
        if (!$user) {
            $this->error('用户不存在');
        }
        $where['user_id'] = $user['id'];
        $where['status'] = 1;
        $list = Db::name('project_list')->where($where)->select();
        $total = Db::name('project_list')->where($where)->count();
        $this->success('',['total'=>$total,'list'=>$list]);
    }
}
